#!/usr/bin/env python

LIWE_PERMS = {}

from user_class import User

#{{{def create_dataset ( start_point, lines, rows, tot_rows ):
def create_dataset ( start_point, lines, rows, tot_rows ):
    res = {}
    
    count = 0
    for v in rows:
        v [ "_pos" ] = start_point + count
        res [ "row%d" % count ] = v
        count += 1

    res [ 'rows' ] = tot_rows
    res [ 'from_row' ] = start_point
    res [ 'to_row' ] = start_point + count
    res [ 'lines' ] = lines

    return res
#}}}
def list_new ( liwe, email = '', enabled = 1, login = '', created = None, last_log = None, _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:

		- email:		SAFE_STRING
		- login:		SAFE_STRING
		- enabled:		SAFE_NUMBER
		- created:             	SAFE_DATE
		- last_log:            	SAFE_DATE
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify users
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	limit = _X_LINES

	uc = User ( liwe )
	res = uc.list_new ( email, enabled, login, created, last_log, start_point, limit )

	count = 0
	for v in res [ 'rows' ]:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v

		v [ '_img' ] = ''

		count += 1

	res [ 'rows' ] = res [ 'tot_rows' ]

	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = _X_LINES

	return res

def list ( liwe ):
	"""
	:INPUT:

		- uid:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify users
	"""
	uc = User ( liwe )
	return { "users" : uc.list () }

def create ( liwe, login, email, uid = -1, pwd = '', enabled = 1 ):
	"""
	:INPUT:

		- uid:		SAFE_NUMBER
		- login:	SAFE_DB_STRING
		- pwd:		SAFE_DB_STRING
		- email:	SAFE_DB_STRING
		- enabled:	SAFE_DB_BOOL

	:PERMS:
		- create:		With this permission you can create/modify users
	"""
	uc = User ( liwe )

	resuid = uc.create ( login = login, passwd = pwd, email = email, enabled = enabled, uid = uid )

	return { "uid" : resuid }

def delete ( liwe, ids ):
	"""
	:INPUT:

		- ids:		SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify users
	"""
	uc = User ( liwe )
	uc.delete ( ids )

def get ( liwe, uid, group = '' ):
	"""
	:INPUT:

		- uid:							SAFE_NUMBER
		- group: if set it will return user related groups	SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify users
	"""
	uc = User ( liwe )
	return { 'user': uc.get_info ( uid, group ) }

def set_enabled ( liwe, uids, enabled = 0 ):
	"""
	:INPUT:

		- uids:		SAFE_STRING
		- enabled: 	SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify users
	"""
	uc = User ( liwe )
	return { 'user': uc.set_enabled ( uids, enabled ) }

def change_pwd ( liwe, uid, pwd ):
	"""
	:INPUT:

		- uid:		SAFE_NUMBER
		- pwd: 		SAFE_DB_STRING

	:PERMS:
		- create:		With this permission you can create/modify users
	"""
	uc = User ( liwe )
	uc.change_pwd ( uid, pwd )

def list_user_session ( liwe, uid = 0, login = '', email = '', quant = 0, _X_LINES = -1, _X_START_POINT = 0, _X_PAGE = 0  ):
	"""
	:INPUT:
		- uid:		SAFE_NUMBER
		- login:	SAFE_DB_STRING
		- email:	SAFE_DB_STRING
		- _X_LINES:     SAFE_NUMBER
		- _X_START_POINT:   SAFE_NUMBER
		- _X_PAGE:      SAFE_NUMBER
	:PERMS:
		- admin:		With this permission you can list user session
	"""

	use_dataset = 0

	if _X_LINES != -1:
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	uc = User ( liwe ) 
	resp = uc.list_user_session ( uid = uid, login = login, email = email, quant = quant, start_point = start_point, do_count = use_dataset )

	rows = resp [ 'rows' ]
	tot_rows =  resp [ 'tot_rows' ]

	if not use_dataset:
		return { 'session': rows }

	return create_dataset ( start_point, quant, rows, tot_rows )

def del_user_session ( liwe, ids = "" ):
    """
    :INPUT:
        -ids: SAFE_DB_STRING
    :PERMS:
		- admin:
        - delete With this permission you can delete user session 
    """
    u = User ( liwe )
    resp =  u.del_user_session ( ids =  ids ) 
    
def user_data_get ( liwe, uid = 0, id_data_type = 0 ):
	"""
	:INPUT:
		- uid:		SAFE_NUMBER
		- id_data_type:	SAFE_NUMBER
	:PERMS:
		- admin	
		- create
	"""
	u = User ( liwe )
	return { 'user_data' : u.user_data_get ( uid = uid, id_data_type = id_data_type ) }	

def user_data_set ( liwe, uid = 0, id_data_type = 0, value = '', id_user_data = 0 ):
	"""
	:INPUT:
		- uid:		SAFE_NUMBER
		- id_data_type:	SAFE_NUMBER
		- value:	SAFE_STRING
		- id_user_data:	SAFE_NUMBER
	:PERMS:
		- admin	
		- create
	"""
	u = User ( liwe )
	res = u.user_data_set ( uid = uid, id_data_type = id_data_type, value = value, id_user_data = id_user_data ) 	
	return { 'user_data' : res } 

def user_data_del ( liwe, id_user_data = 0 ):
	"""
	:INPUT:
		- id_user_data:	SAFE_NUMBER
	:PERMS:
		- admin	
		- create
	"""
	u = User ( liwe )
	u.user_data_del ( id_user_data = id_user_data ) 	

def user_data_type_set ( liwe, name = '', id_data_type = 0 ):
	"""
	:INPUT:
		- name:		SAFE_STRING
		- id_data_type:	SAFE_NUMBER
	:PERMS:
		- admin	
		- create
	"""
	u = User ( liwe )
	return { 'data_type' : u.user_data_type_set ( name = name, id_data_type = id_data_type ) }	

def user_data_type_del ( liwe, id_data_type = 0 ):
	"""
	:INPUT:
		- id_data_type:	SAFE_NUMBER
	:PERMS:
		- admin	
		- create
	"""
	u = User ( liwe )
	u.user_data_type_del ( id_data_type = id_data_type )
